﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveBorrow.Services;
using ePlanet.ArchiveBorrow.Models;
using ePlanet.ArchiveMange.Models;
using System.Data;

namespace Web.Views.ArchiveBorrow.ArchiveBorrowApprove
{
    public partial class EquipmentApprove : PageBase
    {
        [Dependency]
        public IArchiveBorrowService m_IArchiveBorrowService
        {
            get;
            set;
        }

        [Dependency]
        public IEquipmentService m_IArchiveService
        {
            get;
            set;
        }

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        [Dependency]
        public Web.SystemConfig.Services.ICodeTable CodeTableService
        {
            get;
            set;
        }

        private ArchiveBorrowModel.ARCHIVEBORROWRow m_CurrentArchiveBorrow = null;
        public string archiveTypeName;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["DataID"]))
            {
                Guid dataID = new Guid(Request.QueryString["DataID"]);
                this.InitArchiveBorrowData(dataID);
                if (!IsPostBack)
                {
                    this.InitArchiveData();
                }
            }
            else
            {
                Alert("参数DataID不能为空！");
            }
        }

        #region 页面事件
        /// <summary>
        /// 绑定卷内件
        /// </summary>
        /// <param name="dataId"></param>
        private void LoadArchivePageFile(string dataId)
        {
            DataTable dtPageFile = m_IArchiveService.GetPageFileByDATAID(dataId);
            this.gv_PageFileView.DataSource = dtPageFile;
            this.gv_PageFileView.DataBind();
        }

        /// <summary>
        /// 卷内件行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_PageFileView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                System.Web.UI.WebControls.GridViewRow rows = ((GridViewRow)e.Row);
                string DataID = drv["DataID"].ToString();
                //int orderNum = Convert.ToInt32(drv["ORDERNUM"]);
                string url = "PageFileEdit.aspx?archiveId=" + Request.QueryString["DataID"] + "&archiveType=" + this.h_ArchiveTypeId.Value + "&DataID=" + DataID + "&ReadOnly=true";
                e.Row.Cells[5].Text = CodeTableService.GetpkNameByCodepkValue(drv["F_SECURITYTYPE"].ToString());
                e.Row.Cells[6].Text = CodeTableService.GetpkNameByCodepkValue(drv["F_KEEPTIME"].ToString());
                //e.Row.Cells[8].Text = orgService.GetOrgNameByOrgID(drv["FIleOrgID"].ToString());
                e.Row.Cells[9].Text = CodeTableService.GetpkNameByCodepkValue(drv["F_CARRIERTYPE"].ToString());
            }
        }

        /// <summary>
        /// 绑定附件
        /// </summary>
        private void LoadAttachments()
        {
            //DataTable dt_Attach = this.attachArchive.GetSaveDataTable(this.attachArchive.ObjectID, this.attachArchive.ObjectTableName);

            //return BuldProjectArchiveService.SaveAttach(dt_Attach, this.attachArchive.ObjectTableName, this.attachArchive.ObjectID,
            //     this.attachArchive.AttachmentDBHelperString);
        }


        #endregion

        #region 私有方法
        private void InitArchiveData()
        {
            if (this.m_CurrentArchiveBorrow != null)
            {
                ArchiveMangeModal.EQUIPMENT_ARCHIVEDataTable archiveData = this.m_IArchiveService.GetAllDocumenArchivetDataById(this.m_CurrentArchiveBorrow.ARCHIVEDATAID);
                //绑定数据
                if (archiveData != null && archiveData.Rows.Count > 0)
                {
                    this.BindDocumentArchive((ArchiveMangeModal.EQUIPMENT_ARCHIVERow)archiveData.Rows[0]);
                }
            }
        }

        /// <summary>
        /// 绑定页面控件数据
        /// </summary>
        /// <param name="currentArchiveData"></param>
        private void BindDocumentArchive(ArchiveMangeModal.EQUIPMENT_ARCHIVERow row)
        {
            this.txtArchiveNum.Text = row.ARCHIVENUM;
            this.txtArchiveSource.Text = row.ARCHIVESOURCE;
            ArchiveMangeModal.ARCHIVEMENUDataTable dtMenu = ArchiveMenuService.GetArchiveMenuById(row.ARCHIVETYPE.ToString());
            ArchiveMangeModal.ARCHIVEMENURow drMenu = dtMenu.Rows[0] as ArchiveMangeModal.ARCHIVEMENURow;
            this.txtArchiveType.Text = drMenu.ARCHIVEMENU;
            this.archiveTypeName = drMenu.ARCHIVEMENU;
            this.h_ArchiveTypeId.Value = row.ARCHIVETYPE.ToString();
            this.txtBoxNum.Text = row.BOXNUM;
            //DataTable dtOrg = orgService.GetORGInfo(row.FILEORGID);
            //this.txtFileOrgName.Text = dtOrg.Rows[0]["Org_Name"].ToString();
            //this.h_FileOrgID.Value = row.FILEORGID;
            //归档单位改为明文
            this.txtFileOrgName.Text = row.FILEORGID;
            if (!row.IsFILETIMENull())
            {
                this.txtFilTime.Text = row.FILETIME.ToString("yyyy-MM-dd");
            }
            this.txtFullFondsNum.Text = row.FULLFONDSNUM;
            this.txtMainBodyNum.Text = row.MAINBODYNUM.ToString();
            this.txtPublishOrgID.Text = row.PUBLISHORGID;
            this.txtFileNum.Text = row.IsFILENUMNull() ? row.FILENUM.ToString() : "0";
            this.txtPageNum.Text = row.PAGENUM.ToString();
            this.txtRemark.Text = row.REMARK;
            this.txtStartTime.Text = row.STARTTIME.ToString("yyyy-MM-dd");
            this.txtEndTime.Text = row.ENDTIME.ToString("yyyy-MM-dd");
            this.txtSummary.Text = row.SUMMARY;
            this.txtTitle.Text = row.TITLE;
            this.ddlCarrierType.Text = CodeTableService.GetpkNameByCodepkValue(row.F_CARRIERTYPE);
            this.ddlKeepTime.Text = CodeTableService.GetpkNameByCodepkValue(row.F_KEEPTIME);
            this.ddlSecurity.Text = CodeTableService.GetpkNameByCodepkValue(row.F_SECURITYTYPE);
            this.txtStorageType.Text = row.STORAGETYPE;
            this.txtHUjianhao.Text = row.HUJIANHAO;
            this.txtStorageType.Text = row.STORAGETYPE;
            this.txtTypeNum.Text = row.TYPENUM;
            this.ddlKeepSecurityTime.Text = CodeTableService.GetpkNameByCodepkValue(row.F_KEEPSECURITYTIME);

        }

        /// <summary>
        /// 设置页面控件属性
        /// </summary>
        private void SetControlAttribute()
        {

        }


        /// <summary>
        /// 初始化ArchiveBorrow对象
        /// </summary>
        /// <param name="archiveBorrowdataID"></param>
        private void InitArchiveBorrowData(Guid archiveBorrowdataID)
        {
            ArchiveBorrowModel oArchiveBorrowModel = this.m_IArchiveBorrowService.GetSingleArchiveBorrowModelByDataID(archiveBorrowdataID);
            if (oArchiveBorrowModel != null && oArchiveBorrowModel.ARCHIVEBORROW.Rows.Count > 0)
            {
                this.m_CurrentArchiveBorrow = oArchiveBorrowModel.ARCHIVEBORROW[0];
                ((ArchiveBorrowBase)Master).CurrentArchiveBorrow = m_CurrentArchiveBorrow;
            }
        }
        #endregion
    }
}